<?php

namespace backend\controllers;

use zhongkefu\zhongkefu;
use Yii;
use common\models\LinkType\LinkType;
use common\models\LinkType\LinkTypeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\adminlog\AdminLog;
/**
 * LinkTypeController implements the CRUD actions for LinkType model.
 */
class LinkTypeController extends BackendController
{


    /**
     * Lists all LinkType models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new LinkTypeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single LinkType model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, '查看友情链接类别 '.$model->typename);
        return $this->render('view', [
            'model' => $model,
        ]);
    }

    /**
     * Creates a new LinkType model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new LinkType();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$name = Yii::$app->request->post()['LinkType']['typename'];
			Adminlog::addlog(yii::$app->user->id, "添加链接类别 ".$name);
            return $this->redirect(['view', 'id' => $model->lid]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing LinkType model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
			$name = Yii::$app->request->post()['LinkType']['typename'];
			Adminlog::addlog(yii::$app->user->id, "修改链接类别 ".$name);
            return $this->redirect(['view', 'id' => $model->lid]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing LinkType model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		$model->delete();
		Adminlog::addlog(yii::$app->user->id, '删除链接类别 '.$model->typename);
        return $this->redirect(['index']);
    }

    /**
     * Finds the LinkType model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return LinkType the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = LinkType::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
	    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
		$artname = "";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
			foreach($ids as $val){
				$model = LinkType::findOne(['lid' => $val]);
				$artname .= $model->typename.",";
			}
			$artname = substr($artname,0,-1);
            foreach($ids as $lid){
                $model = LinkType::findOne(['lid' => $lid]);
                $this->findModel($lid)->delete();
            }
			Adminlog::addlog(yii::$app->user->id, '批量删除链接类别 '.$artname);
            return true;
        }
    }
	
	
	
}
